package com.jckj.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.NotifyMapper;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.Notify;
import com.jckj.core.entity.NotifyExample;
import com.jckj.core.enums.NotifyStatus;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.NotifyService;
import com.jckj.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 通知公告的服务类
 * </p>
 *
 * @author:liukuan
 * @Date:2018-01-25
 */
@Service
@Transactional(readOnly = true, rollbackFor = ServiceException.class)
public class NotifyServiceImpl implements NotifyService {
    private static Logger LOGGER = LoggerFactory.getLogger(NotifyService.class);

    @Autowired
    private NotifyMapper notifyMapper;


    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public PageInfo<Notify> findAll(String status, Integer pageNum, Integer pageSize) {

        //这段代码表示，程序开始分页了，pageNum默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录                                    ){
        PageHelper.startPage(pageNum, pageSize);
        NotifyExample example = new NotifyExample();
        NotifyExample.Criteria criteria = example.createCriteria();
        if (NotifyStatus.UNPUBLISHED.getCode().equals(status)) {
            criteria.andStatusEqualTo(NotifyStatus.UNPUBLISHED.getCode());
        } else {
            criteria.andStatusEqualTo(NotifyStatus.PUBLISHED.getCode());
        }
        List<Notify> notifyList = notifyMapper.selectByExample(example);
        PageInfo<Notify> p = new PageInfo<Notify>(notifyList);
        return p;
    }


    /**
     * 根据主键查询
     *
     * @param id
     */
    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public Notify findNotifyByPrimaryKey(String id) {
        return notifyMapper.selectByPrimaryKey(id);
    }


    /**
     * 根据任意条件删除的方法
     *
     * @param
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int deleteConditionNotify(String id) {
        int num = 0;
        try {
            num = notifyMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            LOGGER.error("删除通知公告出错", e.fillInStackTrace());
            throw new ServiceException("删除通知公告错误", e);
        }
        return num;
    }


    /**
     * 添加的方法
     *
     * @param notify
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int insertConditionNotify(Notify notify) {
        int num = 0;
        try {
            num = notifyMapper.insertSelective(notify);
        } catch (Exception e) {
            LOGGER.error("新增通知公告:%s", e.fillInStackTrace());
            throw new ServiceException("新增通知公告错误", e);
        }
        return num;
    }


    /**
     * 修改的方法
     *
     * @param notify
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int updateConditionNotify(Notify notify) {
        int num = 0;
        try {
            num = notifyMapper.updateByPrimaryKeySelective(notify);
        } catch (Exception e) {
            LOGGER.error("修改通知公告:%s", e.fillInStackTrace());
            throw new ServiceException("修改通知公告错误", e);
        }
        return num;
    }

    /**
     *
     * @param notify
     * @return
     */
    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public PageInfo<Notify> findSearch(Integer pageNum,Integer pageSize,Notify notify) {
        //这段代码表示，程序开始分页了，pageNum默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录                                    ){
        PageHelper.startPage(pageNum, pageSize);

        NotifyExample example=new NotifyExample();
        NotifyExample.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotBlank(notify.getCode())){
           criteria.andCodeEqualTo(notify.getCode());
        }
        if(StringUtils.isNotBlank(notify.getTitle())){
            criteria.andTitleLike(notify.getTitle()+"%");
        }
        List<Notify> list=notifyMapper.selectByExample(example);
        PageInfo<Notify> p=new PageInfo<Notify>(list);
        return p;
    }


}
